
package com.jojoteamwork.photo.ui;

import com.jojoteamwork.photo.R;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.LinearLayout;
import android.widget.Toast;

import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;

public class SetPasswordActivity extends Activity {
    LinearLayout plPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setpsw_layout);
        initWheel(R.id.passw_1);
        initWheel(R.id.passw_2);
        initWheel(R.id.passw_3);
        initWheel(R.id.passw_4);
        findViewById(R.id.btnSetpsw).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                StringBuilder pswBuilder = new StringBuilder();

                pswBuilder.append(getWheel(R.id.passw_1).getCurrentItem())
                        .append(getWheel(R.id.passw_2).getCurrentItem())
                        .append(getWheel(R.id.passw_3).getCurrentItem())
                        .append(getWheel(R.id.passw_4).getCurrentItem());
                SharedPreferences preferences = getSharedPreferences("setting", MODE_PRIVATE);
                Editor editor = preferences.edit();
                editor.putString("psw", pswBuilder.toString());
                editor.commit();
                Toast.makeText(SetPasswordActivity.this, R.string.setsuccess, Toast.LENGTH_SHORT)
                        .show();
                Intent intent = new Intent(SetPasswordActivity.this, MagazineActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            }
        });

        findViewById(R.id.btnClearpsw).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                SharedPreferences preferences = getSharedPreferences("setting", MODE_PRIVATE);
                Editor editor = preferences.edit();
                editor.putString("psw", "");
                editor.commit();
                Intent intent = new Intent(SetPasswordActivity.this, MagazineActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            }
        });
    }

    /**
     * Initializes wheel
     * 
     * @param id the wheel widget Id
     */
    private void initWheel(int id) {
        WheelView wheel = getWheel(id);
        wheel.setViewAdapter(new NumericWheelAdapter(this, 0, 9));
        wheel.setCurrentItem(0);
        wheel.setCyclic(true);
        wheel.setInterpolator(new AnticipateOvershootInterpolator());
    }

    /**
     * Returns wheel by Id
     * 
     * @param id the wheel Id
     * @return the wheel with passed Id
     */
    private WheelView getWheel(int id) {
        return (WheelView) findViewById(id);
    }

}
